//
//  ZBGiftView.M
//  送礼
//
//  Created by 华惠友 on 2017/9/19.
//  Copyright © 2017年 华惠友. All rights reserved.
//

#import "ZBGiftView.h"
#import "ZBCollectionViewCell.h"
#import "Masonry.h"

static const CGFloat kminimumInteritemSpacing  = 0;
static const CGFloat kminimumLineSpacing  = 0;
static const int kPerPage_HorizontalCellCount = 4;
static const int kPerPage_VerticalCellCount = 2;

static const CGFloat kToolViewHeight  = 60;

static NSString *const kGiftCollectionCell = @"giftCell";

@interface ZBGiftView () <UICollectionViewDelegate, UICollectionViewDataSource> {
    NSInteger _giftSelectIndex;
    NSIndexPath *_selectIndexPath;
}

@property (nonatomic, strong) UICollectionView *giftCollecView;

@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) NSMutableArray *titleArray;

@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UIButton *rechageBtn;
@property (nonatomic, strong) UIButton *sendBtn;

@end

@implementation ZBGiftView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _giftSelectIndex = 0;
        [self setUI];
    }
    return self;
}

- (void)setUI {
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 1)];
    lineView.backgroundColor = [UIColor grayColor];
    [self addSubview:lineView];
    [self addSubview:self.giftCollecView];
    [self addSubview:self.pageControl];
    [self addSubview:self.rechageBtn];
    [self addSubview:self.sendBtn];
    
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.giftCollecView.mas_bottom).offset(5);
        make.height.equalTo(@20);
        make.width.equalTo(@20);
    }];
    
    [self.rechageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(20);
        make.top.equalTo(self.giftCollecView.mas_bottom).offset(30);
        make.bottom.equalTo(self).offset(-10);
        make.width.equalTo(@40);
    }];
    
    [self.sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.rechageBtn);
        make.height.equalTo(self.rechageBtn);
        make.width.equalTo(@100);
        make.right.equalTo(self).offset(-20);
    }];
}

#pragma mark - Touch Event
- (void)rechageMoney:(UIButton *)sender {
    NSLog(@"充值");
}

- (void)sendGift:(UIButton *)sender  {
//    NSLog(@"送礼");
    if (self.giftBlock) { // _giftSelectIndex 为 1000+
        self.giftBlock(_giftSelectIndex);
    }
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imageArray.count;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZBCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kGiftCollectionCell forIndexPath:indexPath];
    cell.giftTitleLabel.text = self.titleArray[indexPath.row];
    cell.giftImage.image = [UIImage imageNamed:self.imageArray[indexPath.row]];
    cell.layer.borderWidth = 0.5;
    cell.layer.borderColor = [UIColor grayColor].CGColor;
    return cell;
}

#pragma mark - UICollectionViewDelegate 
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    // 将之前的选中的单元格边框去除
    if (_selectIndexPath) {
        ZBCollectionViewCell *cell = (ZBCollectionViewCell *)[collectionView cellForItemAtIndexPath:_selectIndexPath];
        cell.layer.borderWidth = 0;
    }
    _selectIndexPath = indexPath;
    _giftSelectIndex = indexPath.row + 1000;
    ZBCollectionViewCell *cell = (ZBCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.layer.borderWidth = 1;
    cell.layer.borderColor = [UIColor blueColor].CGColor;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat scrollViewW = self.frame.size.width;
    CGFloat x = scrollView.contentOffset.x;
    int page = x / scrollViewW;
    self.pageControl.currentPage = page;
}

#pragma mark - 懒加载
- (UICollectionView *)giftCollecView {
    if (!_giftCollecView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        CGFloat width = (self.frame.size.width - (kPerPage_HorizontalCellCount)*kminimumInteritemSpacing) / (kPerPage_HorizontalCellCount * 1.0);
        CGFloat height = (self.frame.size.height - (kToolViewHeight + 3*kminimumLineSpacing)) / (kPerPage_VerticalCellCount * 1.0);
        layout.itemSize = CGSizeMake(width, height);
        
        
        CGRect frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - kToolViewHeight);
        _giftCollecView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];
        _giftCollecView.backgroundColor = [UIColor clearColor];
        
        _giftCollecView.pagingEnabled = YES;
        _giftCollecView.showsHorizontalScrollIndicator = NO;
        _giftCollecView.bounces = NO;
        
        _giftCollecView.delegate = self;
        _giftCollecView.dataSource = self;
        
        [_giftCollecView registerNib:[UINib nibWithNibName:@"ZBCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kGiftCollectionCell];
    }
    return _giftCollecView;
}

//分页
- (UIPageControl *)pageControl{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] init];
        _pageControl.currentPage = 0;
        _pageControl.numberOfPages = 3;
        [_pageControl setCurrentPageIndicatorTintColor:[UIColor whiteColor]];
        [_pageControl setPageIndicatorTintColor:[UIColor grayColor]];
    }
    return _pageControl;
}

- (UIButton *)rechageBtn {
    if (!_rechageBtn) {
        _rechageBtn = [UIButton buttonWithType:0];
        [_rechageBtn setTitle:@"充值" forState:UIControlStateNormal];
        [_rechageBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [_rechageBtn addTarget:self action:@selector(rechageMoney:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rechageBtn;
}

- (UIButton *)sendBtn {
    if (!_sendBtn) {
        _sendBtn = [UIButton buttonWithType:0];
        _sendBtn.backgroundColor = [UIColor blueColor];
        [_sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _sendBtn.layer.cornerRadius = (kToolViewHeight - 40) / 2.0;
        _sendBtn.layer.masksToBounds = YES;
        [_sendBtn setTitle:@"发送" forState:UIControlStateNormal];
        [_sendBtn addTarget:self action:@selector(sendGift:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sendBtn;
}


 //礼物图片
- (NSMutableArray *)imageArray{
if (!_imageArray) {
     _imageArray = [NSMutableArray array];
     for (NSInteger i = 0; i < 24; i++) {
         NSString *imagePath=[NSString stringWithFormat:@"yipitiezhi0%zd",i + 1];
         [_imageArray addObject:imagePath];
     }
    }
 return _imageArray;
}

- (NSMutableArray *)titleArray {
    if (!_titleArray) {
        _titleArray = [NSMutableArray arrayWithObjects:@"轨迹轮船", @"粒子花花" , @"帧动画钻石" , @"重力❤️" , @"UIView法拉利" , @"镜子" , @"花圈" , @"围巾" , @"领结" , @"气球" , @"麦克风" , @"小草" , @"帽子" , @"太阳" , @"小马", @"兔帽", @"领结" , @"气球" , @"麦克风" , @"小草" , @"帽子" , @"太阳" , @"小马", @"兔帽", nil];
    }
    return _titleArray;
}








@end
